package com.leapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class BdAdapter {
	private SQLiteDatabase database;
	private BdHelper bdHelper;
	private String[] allColumns = { BdHelper.ID, BdHelper.ENUNCIADO, BdHelper.RESPOSTA};

	public BdAdapter(Context context) {
		bdHelper = new BdHelper(context);
	}
	
	public void open() throws SQLException {
		database = bdHelper.getWritableDatabase();
	}

	public void close() {
		bdHelper.close();
	}
	
	public Questao criaQuestao(String enunciado, String resposta) {
		ContentValues values = new ContentValues();
		values.put(bdHelper.ENUNCIADO, enunciado);
		values.put(bdHelper.RESPOSTA, resposta);
			
		long insertId = database.insert(bdHelper.TABLE_NAME, null, values);
		// To show how to query
		Cursor cursor = database.query(bdHelper.TABLE_NAME, allColumns, bdHelper.ID + " = " + insertId, null,null,null,null);
		cursor.moveToFirst();
		return cursorParaQuestao(cursor);
	}
	
	public void EliminaQuestao (int idQuestao){
		//database.delete(DB.TABLE_NAME, "id=?", new String [] {Integer.toString(idContacto)});
		database.delete(bdHelper.TABLE_NAME, bdHelper.ID + " = " + idQuestao, null);
		}
	private Questao cursorParaQuestao(Cursor cursor) {
		Questao questao = new Questao(cursor.getLong(0),cursor.getString(1),cursor.getString(2));
		return questao;
	}

	public Cursor getQuestoes(){
			Cursor cursor = database.rawQuery("select id, enunciado, resposta from questionario", null);
			return cursor;
		}
	public Questao getQuestao (int idQuestao){
		Cursor cursor = database.query(bdHelper.TABLE_NAME, allColumns, bdHelper.ID + " = " + idQuestao, null,null, null, null);
			cursor.moveToFirst();
		return cursorParaQuestao(cursor);
		}


}
